package com.example.project.activity.kaishi;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.example.project.R;
import com.example.project.tool.SharePreferce;
import com.example.project.volley.ConstantUtil;
import com.example.project.volley.Params;
import com.example.project.volley.VolleyInterface;
import com.example.project.volley.VolleyRequest;
//更新密码页面
public class AlterPassUpdateActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText etPass1,etPass2; //修改密码框
    private Button btYes,btNo; //确定。取消按钮
    private ImageView ivGou; //验证-图片
    private TextView tvGou; //验证-文本
    private ProgressDialog pd; //消息提示框

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_pass_update);
        initView();
    }

    private void initView() {
        ivGou=findViewById(R.id.iv_gou);
        tvGou=findViewById(R.id.tv_gou);
        etPass1=findViewById(R.id.et_pass1);
        etPass2=findViewById(R.id.et_pass2);
        btYes=findViewById(R.id.bt_yes);
        btNo=findViewById(R.id.bt_no);
        btYes.setOnClickListener(this);
        btNo.setOnClickListener(this);
        pd=new ProgressDialog(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_yes:
                String pass1=etPass1.getText().toString().trim();
                String pass2=etPass2.getText().toString().trim();
                if(pass1.length()<6){
                    Toast.makeText(AlterPassUpdateActivity.this,"你输入的密码小于6位数",Toast.LENGTH_LONG).show();
                    ivGou.setImageResource(R.drawable.gou2);
                    tvGou.setTextColor(getResources().getColor(R.color.colorAccent));
                    break;
                }else if(pass1.length()>25){
                    ivGou.setImageResource(R.drawable.gou2);
                    tvGou.setTextColor(getResources().getColor(R.color.colorAccent));
                    Toast.makeText(AlterPassUpdateActivity.this,"你输入的密码大于25位数",Toast.LENGTH_LONG).show();
                    break;
                }
                if(pass1.equals(pass2)){
                    pd = new ProgressDialog(this);
                    pd.setTitle("请稍等");
                    pd.setMessage("正在修改密码......");
                    pd.show();
                    ivGou.setImageResource(R.drawable.gou3);
                    tvGou.setTextColor(getResources().getColor(R.color.lv));
                    String email=SharePreferce.getInstance(AlterPassUpdateActivity.this).getString("email");
                    volleyUpdatePass(pass1,email);
                }else{
                    Toast.makeText(AlterPassUpdateActivity.this,"你输入的两次密码不相同",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.bt_no:
                finish();
                break;
        }
    }
    //修改密码
    private void volleyUpdatePass(String pass,String email) {
          VolleyRequest.RequesetPost( ConstantUtil.UPDATAPASS, "GETEMAIL", Params.GetString2(pass,email),new VolleyInterface( this ){
            @Override
            public void onMySuccess(String result) {
                pd.dismiss();
                Intent intent = new Intent(AlterPassUpdateActivity.this, LoginActivity.class);
                startActivity(intent);
                Toast.makeText(AlterPassUpdateActivity.this,"修改密码成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onMyError(VolleyError error) {
                pd.dismiss();
                Toast.makeText(AlterPassUpdateActivity.this, "修改密码失败", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onImageSuccess(Bitmap bitmap) {
            }
        } );

    }

}